<?php
namespace Home\Controller;

class IndexController extends CommonController{
    
    public function index(){
        //个人信息
        $userInfo = M('User')->find($this->uid);
        if($userInfo['type']=='1' || $userInfo['type']=='3')
            $this->redirect('Index/doctor');//去医生首页
        $this->assign('userInfo',$userInfo);
        //透析须知
        $this->assign('know',M('Know')->find(1));
        //今日提醒文案
        $this->assign('words',M('Words')->field('notice')->find(1));
        //最后一次体重值
        $this->assign('lastWeight',M('Weight')->where(array('uid'=>$this->uid))->order('id desc')->find());
        //未读消息数
        $this->assign('noReadNum',M('Chat')->where(array('sender'=>array('neq','system'),'receiver'=>$userInfo['phone'],'has_read'=>'0'))->count());
        
		$this->display();
	}
    
    //医生或营养师主页
    public function doctor(){
        $User = M('User');
        $Weight = M('Weight');
        //用户信息
        $userInfo = $User->find($this->uid);
        $this->assign('userInfo',$userInfo);
        $field = $userInfo['type'] == '1' ? 'uid' : 'nid';//医生为uid字段营养师为nid
        
        //新闻列表
        $p = I('request.p') ? I('request.p') : 1;
        $list = M('News')->order('id desc')->page($p.',5')->select();
        foreach($list as $k=>$v){
            $list[$k]['name'] = cut_str($v['name'],10);
            $list[$k]['content'] = cut_str(delete_html(htmlspecialchars_decode($v['content'])),25);
        }
        $this->assign('newsList',$list);
        
        //拥有患者数
        $sefeWeightNum = 0;//超重人数
        $patientList = $User->where(array('type'=>'2',$field=>$this->uid,'name'=>array('neq','')))->select();
        if($userInfo['type'] == '1'){//若为医生统计超重人数
            foreach($patientList as $v){
                $weightRow = $Weight->where(array('uid'=>$v['id']))->order('id desc')->find();
                if($v['weight_safe'] > $weightRow['vol']) $sefeWeightNum++;
            }
        }
        $this->assign('patientNum',count($patientList));
        $this->assign('sefeWeightNum',$sefeWeightNum);
        
        //未读消息数
        $this->assign('noReadNum',M('Chat')->where(array('receiver'=>$userInfo['phone'],'has_read'=>'0'))->count());
        $this->display();
	}
    
    //新闻详情页
    public function getNews(){
        //新闻列表
        $p = I('request.p') ? I('request.p') : 1;
        $list = M('News')->order('id desc')->page($p.',5')->select();
        foreach($list as $k=>$v){
            $list[$k]['name'] = cut_str($v['name'],10);
            $list[$k]['content'] = cut_str(strip_tags(htmlspecialchars_decode($v['content'])),25);
        }
        
        if(empty($list)){
            echo json_encode('err');die;
        }else{
            echo json_encode($list);die;
        }
	}
    
    //新闻详情页
    public function news(){
        $this->assign('vo',M('News')->find(I('get.id')));
		$this->display();
	}
    
    //医生或营养师添加患者手机号
    public function addPatientPhone(){
        $User = M('User');
        $phone = I('post.phone');
        if($row = $User->where(array('phone'=>$phone))->find()){
            die('cf');
        }else{
            //用户信息
            $userInfo = $User->find($this->uid);
            $field = $userInfo['type'] == '1' ? 'uid' : 'nid';//医生修改uid字段营养师修改nid
            
            if($res = $User->add(array($field=>$this->uid,'phone'=>$phone,'type'=>'2'))){
                send_add_msg($phone);//发送短信
                die('suc');
            }else{
                die('err');
            }
        }
    }
    
    public function test(){
        session('fromuser',null);
    }
    
}